

#pragma once

#include "../qfile.h"
using namespace micro::files;

#include "../qcom.h"
using namespace micro::com;




#include <xmllite.h>
#pragma comment(lib, "Xmllite.lib")



namespace micro{
namespace qxml{



class QXmlBase: public QFile
{
protected:
	QString m_file;
	QComPointer<IStream> m_pFileStream;

public:
	QXmlBase(){};
	~QXmlBase(){};



};


class QXmlWriter: public QXmlBase
{
protected:
	 QComPointer<IXmlWriter> m_pWriter;

public:
	QXmlWriter();
	~QXmlWriter();


};


class QXmlReader: public QXmlBase
{
protected:
	 QComPointer<IXmlReader> m_pReader;

public:
	QXmlReader();
	~QXmlReader();


};












enum QTagType
{
	QTAG,
	QATTR,
	QTEXT

};


struct QxmlNode;

struct QxmlList
{

	QxmlNode  *  head;
	QxmlNode  *  tail;
	int     contor; 

	QxmlList()
	{
		contor = 0;
		head = tail = nullptr;
	}
};

struct QxmlNode
{
	QString		nume;
	QTagType	tip;
	QString		valoare;

	QxmlNode*   next;
	
	QxmlList	children;
	
public:
	QxmlNode()
	{
		next = nullptr;
	}
	~QxmlNode()
	{
		if(0 == children.contor)
			return;

		QxmlNode *q, * p = children.head;
		while(p)
		{
			q = p;
			p = p->next;
			delete q;
		}
		 
	
	}
};









class QXMLParser
{
	struct Buffer
	{	
		TCHAR *	date;
		UINT	len;
		UINT	pos;	
	};

	void	Add(QxmlNode * root, QxmlNode* fiu);	
	bool	isWhite(TCHAR a);
	void	SkipWhite(Buffer * buf);
	bool	Expect(Buffer * buf, TCHAR a);
	TCHAR	Next(Buffer * buf);

	QxmlNode *  GetRoot(Buffer *buf);


protected:
	QxmlNode *  m_root;

protected:
	QxmlNode *	FindTag(const QString & str, QxmlNode * root);


public:
	QXMLParser(){ m_root = nullptr;	}
	~QXMLParser(){ if (m_root) delete m_root;}	 
	bool ParseInput(TCHAR *buff, int len);
public:
	bool		GetList(const QString & str, QStringArray & list);
	long		GetLongAttr(const QString & tag, const QString & attr, long def );

	QxmlNode *	GetTagNode(const QString & tag);
	QxmlNode *	GetFirstTag(QxmlNode *node, const QString & tag);
	QxmlNode *	GetNextTag(QxmlNode *node, const QString & tag);

	QString		GetStrAttr(QxmlNode *node, const QString & attr);
	long		GetLongAttr(QxmlNode *node , const QString & attr);





};


 


}//
}//micro